package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


import com.neuedu.oasystem.dao.IPlanAuditDao;
import com.neuedu.oasystem.dao.impl.PlanAuditDaoImpl;
import com.neuedu.oasystem.entity.PlanAudit;
import com.neuedu.oasystem.service.PlanAuditService;
import com.neuedu.oasystem.util.DBHelper;

public class PlanAuditServiceImpl implements PlanAuditService{

	private IPlanAuditDao planAuditDao;

	public PlanAuditServiceImpl() {
		super();
		this.planAuditDao = new PlanAuditDaoImpl();
	}
	@Override
	public boolean add(PlanAudit param) {
		Connection connection = DBHelper.openConnection();
		try {
			connection.setAutoCommit(false);
			
			planAuditDao.setConnection(connection);
			boolean ret = planAuditDao.add(param);
			
			connection.commit();
			connection.setAutoCommit(true);
			return ret;
		} catch (Exception e) {
			if(connection!=null) {
				try {
					connection.rollback();
					connection.setAutoCommit(true);
				} catch (Exception e2) {
				}	
			}
		} finally {
			DBHelper.closeAll(null, null, connection);
		}
		return false;
	}

	@Override
	public boolean edit(PlanAudit param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			planAuditDao.setConnection(conn);
			boolean ret = planAuditDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public PlanAudit findById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			planAuditDao.setConnection(conn);
			PlanAudit audit = planAuditDao.findById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return audit;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<PlanAudit> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			planAuditDao.setConnection(conn);
			List<PlanAudit> list = planAuditDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
			
		}
		
		return null;
	}

	@Override
	public List<PlanAudit> findByExample(PlanAudit param) {
		Connection conn = DBHelper.openConnection();
		try {
			planAuditDao.setConnection(conn);
			List<PlanAudit> list = planAuditDao.findByExample(param);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<PlanAudit> findByPager(int page, int size) {
		Connection conn = DBHelper.openConnection();
		try {
			planAuditDao.setConnection(conn);
			List<PlanAudit> list = planAuditDao.findByPager(page, size);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		Connection conn = DBHelper.openConnection();
		try {
			planAuditDao.setConnection(conn);
			int id  = planAuditDao.deleteByIds(ids);
			return id;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int addAll(List<PlanAudit> useparamrs) {
		Connection conn = DBHelper.openConnection();
		try {
			planAuditDao.setConnection(conn);
			int all  = planAuditDao.addAll(useparamrs);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int editAll(List<PlanAudit> param) {
		// TODO Auto-generated method stub
		return 0;
	}

}
